package com.sneakxy.mybase4j.platform.provider;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;

import com.alibaba.dubbo.config.annotation.Service;
import com.sneakxy.mybase4j.base.provider.BaseProvider;
import com.sneakxy.mybase4j.platform.beans.Dict;
import com.sneakxy.mybase4j.platform.repository.DictRepository;
import com.sneakxy.mybatis.commons.repository.query.domain.CriteriaBuilder;

/**
 * 
 * @author 潜行的青衣
 * @see
 */
@Service(version = "1.0.0", interfaceClass = IDictProvider.class)
@org.springframework.stereotype.Service
public class DictProvider extends BaseProvider<Dict, String, DictRepository> implements IDictProvider {

	@Autowired
	public DictProvider(DictRepository repository) {
		super(repository);
	}

	/* (non-Javadoc)
	 * @see com.sneakxy.mybase4j.platform.provider.IDictProvider#findByType(java.lang.String, java.lang.String, boolean)
	 */
	@Override
	public Dict findByType(String parent_id, String value, boolean children) {
		final CriteriaBuilder<Dict> builder = new CriteriaBuilder<Dict>(Dict.class);
		if(StringUtils.isBlank(parent_id)) {
			builder.andIsNull("parentId");
		} else {
			builder.andEq("parentId", parent_id);
		}
		builder.andEq("value", value);
		Dict result = this.getRepository().findOne(builder.build());
		if(children && result != null) {
			result.setChildren(this.findDicts(result.getId(), true));
		}
		return result;
	}
	
	public List<Dict> findDicts(String parent_id, boolean children) {
		final CriteriaBuilder<Dict> builder = new CriteriaBuilder<Dict>(Dict.class);
		if(StringUtils.isBlank(parent_id)) {
			builder.andIsNull("parentId");
		} else {
			builder.andEq("parentId", parent_id);
		}
		final Sort sort = new Sort(new Order(Direction.ASC, "sort"));
		List<Dict> result = this.getRepository().findAll(builder.build(), sort);
		if(children) {
			Iterator<Dict> it = result.iterator();
			Dict dict = null;
			while(it.hasNext()) {
				dict = it.next();
				dict.setChildren(this.findDicts(dict.getId(), true));
			}
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see com.sneakxy.mybase4j.platform.provider.IDictProvider#findByValues(java.lang.String[], boolean)
	 */
	@Override
	public Dict findByValues(String[] values, boolean children) {
		String parent_id = null;
		Dict dict = null;
		for(String value:values) {
			dict = this.findByType(parent_id, value, false);
			if(dict == null) {
				break;
			}
			parent_id = dict.getId();
		}
		if(children && dict != null) {
			dict.setChildren(this.findDicts(parent_id, children));
		}
		return dict;
	}

	/* (non-Javadoc)
	 * @see com.sneakxy.mybase4j.platform.provider.IDictProvider#findBySearch(java.lang.String, java.lang.String, org.springframework.data.domain.Pageable)
	 */
	@Override
	public Page<Dict> findBySearch(String parent_id, String search, Pageable pageable) {
		final CriteriaBuilder<Dict> builder = new CriteriaBuilder<Dict>(Dict.class);
		if(StringUtils.isNotBlank(search)) {
			String value = new StringBuilder().append("%").append(search).append("%").toString();
			builder.orLike("label", value);
			builder.orLike("memo", value);
			builder.orLike("value", value);
		}
		if(StringUtils.isBlank(parent_id)) {
			builder.andIsNull("parentId");
		} else {
			builder.andEq("parentId", parent_id);
		}
		return this.findAll(builder.build(), pageable);
	}

}
